const { query, param } = require("express-validator");
const mongoose = require("mongoose");

const pageno = query("pageno")
    .optional()
    .isInt({ min: 1 })
    .withMessage("页码号最小为1")
    .toInt()
    .withMessage("页码号必须为正整数");

const pagesize = query("pagesize")
    .optional()
    .isInt({ min: 1 })
    .withMessage("页大小最小为1")
    .toInt()
    .withMessage("页大小必须为正整数");

const id = param("id").custom((value) => {
    if (!mongoose.Types.ObjectId.isValid(value)) {
        throw new Error("无效的id");
    }
    return true;
});

module.exports = { pageno, pagesize, id };
